package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    //分类分页查询
    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result pageCategory(CategoryPageQueryDTO dto) {
        log.info("分类分页查询：{}", dto);
        PageResult result = categoryService.list(dto);
        // log.info("获得数据封装结果{}", result);
        return Result.success(result);
    }
    @PutMapping
    @ApiOperation(" 修改分类")
    public Result updateCategory(@RequestBody CategoryDTO dto) {
        log.info("修改分类{}", dto);
        categoryService.updateCategory(dto);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用分类")
    public Result statusUpdate(@PathVariable Integer status, Long id) {
        categoryService.statusUpdate(status,id);
        return Result.success();
    }
    @ApiOperation("新增分类")
    @PostMapping
    public Result insertCategory(@RequestBody CategoryDTO dto) {
        log.info("新增分类：{}",dto);
        categoryService.insertCategory(dto);
        return Result.success();
    }

    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result deleteCategory(Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result getList(int type){
        log.info("获取类型{}", type);
      List<Category> result= categoryService.getList(type);
      return Result.success(result);
    }
}
